创建Excel图表
IronXL for Python支持在现代XLSX文件格式的Excel文档中创建和编辑图表。
此示例演示如何创建折线图。 还支持其他图表类型。
如何在Python中创建Excel折线图
- 安装IronXL for Python以创建Excel折线图。
- 加载 Excel 工作簿并使用
CreateChart创建图表。 - 将所需的数据值添加到
WorkBook。 - 设置图表的标题和图例,然后调用
Plot方法来呈现图表。 - 将
WorkBook数据保存到Excel文件。
from ironxl import *
# Load the Excel workbook
workbook = WorkBook.Load("test.xlsx")
worksheet = workbook.DefaultWorkSheet
# Set the chart type and its position on the worksheet.
chart = worksheet.CreateChart(ChartType.Line, 10, 10, 18, 20)
# Add the series to the chart
# The first parameter represents the address of the range for the horizontal(category) axis.
# The second parameter represents the address of the range for the vertical(value) axis.
series = chart.AddSeries("B3:B8", "A3:A8")
# Set the chart title.
series.Title = "Line Chart"
# Set the legend position.
# Can be removed by setting it to None.
chart.SetLegendPosition(LegendPosition.Bottom)
# We can change the position of the chart.
chart.Position.LeftColumnIndex = 2
chart.Position.RightColumnIndex = chart.Position.LeftColumnIndex + 3
# Plot all the data that was added to the chart before.
# Multiple calls to this method lead to plotting multiple charts instead of modifying the existing chart.
# Yet there is no possibility to remove a chart or edit its series/position.
# We can just create a new one.
chart.Plot()
# Save changes with the created line chart
workbook.SaveAs("CreateLineChart.xlsx")IronXL for Python支持在现代XLSX文件格式的Excel文档中创建和编辑图表。
此示例演示如何创建折线图。 还支持其他图表类型。
CreateChart创建图表。WorkBook。Plot方法来呈现图表。WorkBook数据保存到Excel文件。